package com.lf.cloud.grab.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

public class CommMethod {
	
	
	public static String UPLOADTYPE = "1"; //0-本地 , 1-FTP
	public static String LOCALPATH = "D://Manipula/Manipula/pic/aimeishishang/";
	
	
	
	public static Document getDocument(String urlStr) {
		Document document = null;
		try {
			document = Jsoup.connect(urlStr).userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36").get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return document;
	}
	public static Document getDocument(String urlStr , String pageNum) {
		Document document = null;
		try {
			String url = urlStr;
			if(StringUtils.isNotEmpty(pageNum)){
				url +=   pageNum;
			}
			document = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36").get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return document;
	}
	
//	http://www.aimeishishang.com/ArisaraKarbdecho/
	
	
	
	
	/**
	 * @param document
	 * 
	 * @return
	 */
	public static List<String> getDivHrefList(Document document) {
		Element elementById = document.getElementById("waterfall");
		
		Elements elementsByAttributeValue = elementById.getElementsByAttributeValue("class","wxm-imglist-box");
		
		if(elementsByAttributeValue==null || elementsByAttributeValue.size() == 0 ){
			elementsByAttributeValue = elementById.getElementsByTag("li");
		}
		
		
		List<String> linkList = new ArrayList<String>();
		if(elementsByAttributeValue!=null && elementsByAttributeValue.size()>0){
			for (Element element : elementsByAttributeValue) {
				Node childNode = element.childNode(1).childNode(1);
				String linkadd = childNode.attr("href");
				linkList.add(linkadd);
			}
		}
		return linkList;
	}
	
	
	public static List<String> getDocumentPageNum(Document document) {
		//Document document = getDocument(urlStr);
		
		List<String> list = new ArrayList<String>();
		Element elementById = document.getElementById("fd_page_bottom");
		if(elementById!=null){
			Elements pageLast = document.getElementsByAttributeValue("class", "last");
			String totalPage = "";
			String addPrefix = "";
			if(pageLast.size()>0){
				String attr = pageLast.get(0).attr("href");
				String[] split = attr.split("/");
				totalPage = split[split.length-1];
				addPrefix = attr.substring(0, attr.indexOf(totalPage));
			}
			for (int i = 1 ; i <= Integer.parseInt(totalPage) ; i++  ) {
				String pageUrl = addPrefix + i;
				list.add(pageUrl);
			}
		}
		return list;
	}
	public static List<String> getDocumentPageNum(Document document , String pageNum) {
		//Document document = getDocument(urlStr);
		
		List<String> list = new ArrayList<String>();
		Element elementById = document.getElementById("fd_page_bottom");
		if(elementById!=null){
			Elements pageLast = document.getElementsByAttributeValue("class", "last");
			String totalPage = "";
			String addPrefix = "";
			if(pageLast.size()>0){
				String attr = pageLast.get(0).attr("href");
				String[] split = attr.split("/");
				totalPage = split[split.length-1];
				addPrefix = attr.substring(0, attr.indexOf(totalPage));
			}
			int i = 1;
			if(StringUtils.isNotEmpty(pageNum)){
				i = Integer.parseInt(pageNum);
			}
			
			for (; i <= Integer.parseInt(totalPage) ; i++  ) {
				String pageUrl = addPrefix + i;
				list.add(pageUrl);
			}
		}
		return list;
	}
	
	
	
	
	/**
	 * 
	 * 获取到文件 上传至FTP
	 * 
	 * 
	 * @param url
	 */
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
//		String urlStr = "http://www.aimeishishang.com/ArisaraKarbdecho/";
//		Document document = getDocument(urlStr);
//		List<String> documentPageNum = getDocumentPageNum(document);
//		List<String> divHrefList = getDivHrefList(document);
		
		String urlStr = "http://www.aimeishishang.com/ArisaraKarbdecho/";
		
		urlStr = "http://www.aimeishishang.com/gaoqingjingxuan/";
		urlStr = "http://www.aimeishishang.com/MunkawChaosGirl/";
		urlStr = "http://www.aimeishishang.com/cherry_quahst/";
		urlStr = "http://www.aimeishishang.com/xingganmeinv/";
		try {
			//getImgUpload(urlStr , "" ,"" , "");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//String divurl = "http://www.aimeishishang.com/ArisaraKarbdecho/mm4543/";
		
		//getImgUpload(divurl);
		
		
	}

}
